package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.MenuTypeItemAdapter;
import com.dlc.felear.xiaoensale.entity.ActionStepItem;

import java.util.ArrayList;
import java.util.List;


public class PowerSelectDialog extends Dialog implements AdapterView.OnItemClickListener {
    private static final String TAG = "SelectGridviewDialog";
    private GridView gv;
    private Context context;
    private AppCompatActivity activity;
    private PowerSelectDialogCallback callback;
    private ActionStepItem actionStepItem;
    private MenuTypeItemAdapter adapter;

    public PowerSelectDialog(@NonNull Context context, PowerSelectDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }


    public PowerSelectDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        this.activity = (AppCompatActivity) context;
        View root = LayoutInflater.from(context).inflate(R.layout.power_select_gridview_dialog, null);
        gv = root.findViewById(R.id.gv);
        adapter = new MenuTypeItemAdapter(context);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
    }


    public void showDialog(ActionStepItem actionStepItem) {
        this.actionStepItem=actionStepItem;
        adapter.setItems(getPowerLevel());
        show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }


    private void confirmData() {
        dismiss();
    }


    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            String value = adapter.getData().get(position);
            if (callback != null) {
                callback.onItemClickResult(actionStepItem,Integer.parseInt(value.substring(0,1)));
            }
            confirmData();
        } catch (Exception e) {
            showToast(e.getMessage());
            confirmData();
        }

    }

    public interface PowerSelectDialogCallback {
        void onItemClickResult(ActionStepItem actionStepItem, int result);
    }

    private List<String>  getPowerLevel(){
        List<String> temp=new ArrayList<>();
        temp.add("1档");
        temp.add("2档");
        temp.add("3档");
        temp.add("4档");
        temp.add("5档");
        temp.add("6档");
        temp.add("7档");
        temp.add("8档");
        return temp;
    }
}
